/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package palma.services;

import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.db.DatabaseConfiguration;
import com.yammer.dropwizard.db.ManagedDataSourceFactory;
import com.yammer.dropwizard.views.ViewBundle;
import java.io.IOException;
import java.sql.Connection;
import palma.resources.ReferenceResource;
import palma.resources.StokiesResource;



/**
 *
 * @author fikri
 */
public class PalmaService extends Service<PalmaConfiguration> {

    @Override
    public void initialize(Bootstrap btstrp) {
        btstrp.addBundle(new ViewBundle());
    }

    @Override
    public void run(PalmaConfiguration t, Environment environment) throws Exception {
        
        final String host = t.getHost();
        final int port = t.getPort();
        final String username = t.getUsername();
        final String password = t.getPassword();
        final String url = t.getUrl();
        final String driver = t.getDriverClass();
       
       try {
//            ConnectionFactory connectionFactory = createConnectionFactory(username, password, host, port);
//            environment.addHealthCheck(new AMQPHealthCheck(connectionFactory));
//            environment.addResource(new StockQuoteResource(connectionFactory, amqpConfiguration));
           Connection conn = createConnection(username, password, host, port, url, driver);
           environment.addResource(new StokiesResource(conn));           
           environment.addResource(new ReferenceResource(conn));           
           
        } catch (IOException e) {
            
        }
    }
    
    private Connection createConnection(String userName, 
            String password, String hostName, int portNumber, String url , String driver) throws IOException {
        
//        ConnectionFactory factory = new ConnectionFactory();
//        factory.setConnectionTimeout(1000);//times out in 1s.
//        factory.setUsername(userName);
//        factory.setPassword(password);
//        factory.setHost(hostName);
//        factory.setPort(portNumber);
//        return factory;
        
        
        Connection conn = null;
        try{
            
            DatabaseConfiguration conf = new DatabaseConfiguration();
            conf.setUser(userName);
            conf.setPassword(password);
            conf.setUrl(url);
            conf.setDriverClass(driver);

            ManagedDataSourceFactory factory = new ManagedDataSourceFactory();
            conn = factory.build(conf).getConnection();
            
           
//             Properties prop1 = new Properties();
//                    Properties prop2 = new Properties();
//                    Properties prop3 = new Properties();
//                    prop1.setProperty("date format","iso");
//                    prop2.setProperty("time format","iso");
//                    prop3.setProperty("setTransactionIsolation","SERIALIZABLE");
//                    Context initContext = new InitialContext();
//                    initContext.addToEnvironment("date format",prop1);
//                    initContext.addToEnvironment("time format",prop2);
//                    initContext.addToEnvironment("setTransactionIsolation", prop3);
//                    conn = ((DataSource)initContext.lookup("java:comp/env/jdbc/sainsDB")).getConnection();
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
        
    }
    
    
     public static void main(String[] arguments) throws Exception {
        new PalmaService().run(arguments);
    }
    
}
